﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDauGia.Domain.Abstract;
using WebDauGia.Domain.Models;

namespace WebDauGia.Controllers
{
    public class AdminSanPhamController : Controller
    {
        //
        // GET: /AdminSanPham/

        public WebDauGiaDBContext db = new WebDauGiaDBContext();
        private WebRepository repository;
        public AdminSanPhamController(WebRepository repo)
        {
            repository = repo;
        }
        public ActionResult Index()
        {
            return View(repository.SanPhams);
        }
        public ViewResult CreateSanPham(SanPham sp)
        {
            SetLoaiSPViewBag(sp.idloaisp);
            return View("EditSanPham", new SanPham());
        }
        public ViewResult EditSanPham(int id)
        {
            //LoaiSanPham loaisp = repository.LoaiSanPhams.Single(x => x.id == id);
            
            SanPham sp = repository.SanPhams
                .FirstOrDefault(x => x.id == id);

            SetLoaiSPViewBag(sp.idloaisp);
            return View(sp);
        }
        [HttpPost]
        public ActionResult EditSanPham(SanPham sp,HttpPostedFileBase image)
        {
           // try
            //{
                if (ModelState.IsValid)
                {
                    if (image != null) {
                        sp.ImageMimetype = image.ContentType;
                        sp.ImageData = new byte[image.ContentLength];
                        image.InputStream.Read(sp.ImageData, 0, image.ContentLength);
                    }
                    repository.SaveSanPham(sp);
                    TempData["message"] = string.Format("{0} đã được lưu", sp.ten);
                    SetLoaiSPViewBag(sp.idloaisp);
                    return RedirectToAction("Index");
                }
                
        
            //}
            
           // catch(Exception ex)
            //{
            else{
                //có lỗi xảy ra - something wrong with the data values
              //  throw ex;
                return View(sp);
            }
        }
        public ActionResult Delete(int id) {
            SanPham sp = db.SanPhams.Find(id);
            return View(sp);
        }
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteSanPham(int id)
        {
            WebDauGia.Domain.Models.SanPham deletedsp = repository.DeleteSanPham(id);
            if (deletedsp != null)
            {
                TempData["message"] = string.Format("{0} đã được xoá", deletedsp.ten);
            } return RedirectToAction("Index");
        }

        private void SetLoaiSPViewBag(int? id = null)
        {
            if (id == null)
                ViewBag.LoaiSanPhams = new SelectList(repository.LoaiSanPhams, "id", "ten");
            else
                ViewBag.LoaiSanPhams = new SelectList(repository.LoaiSanPhams.ToArray(), "id", "ten", id);

        }
        public FileContentResult GetImage(int id)
        {
            SanPham sp = repository.SanPhams.FirstOrDefault(p => p.id == id);
            if (sp != null)
            {
                return File(sp.ImageData, sp.ImageMimetype);
            }
            else { return null; }
        }

    }
}
